<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            display: none;
            position: fixed;
            width: 120px;
            border: 1px solid #dadce0;
        }

        .box li {
            list-style: none;
            width: 110px;
            float: left;
            line-height: 30px;
            padding-left: 10px;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li>复制</li>
            <li>粘贴</li>
            <li>剪切</li>
        </ul>
    </div>
    <script>
        // 右键：在网页中都是默认事件


        // 右键事件：contextmenu
        document.addEventListener('contextmenu', function (e) {
            console.log('右键事件发生了');

            // 阻止默认事件：e.preventDefault()
            e.preventDefault();

            // 自定义菜单
            let box = document.querySelector('.box');
            box.style.display = 'block';

            // 改变位置
            box.style.left = e.pageX + 'px';
            box.style.top = e.pageY + 'px';

        });

        document.addEventListener('click', function () {
            let box = document.querySelector('.box');
            box.style.display = '';
        });
    </script>
</body>

</html>